package modulo1.cci;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import modulo1.cgt.LoginApl;

@WebServlet(name = "LoginCtrl", urlPatterns = {"/Login"})
public class LoginCtrl extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String logout = request.getParameter("logout");
        if (logout != null) {
            HttpSession session = request.getSession();
            session.invalidate();
        }
        RequestDispatcher cih = request.getRequestDispatcher("login.jsp");
        cih.forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String cpf = request.getParameter("cpf");
        String senha = request.getParameter("senha");
        RequestDispatcher cih = null;
        try {
            if (LoginApl.getInstance().consultarProfessor(cpf, senha)) {
                HttpSession session = request.getSession();
                session.setAttribute("usuario", true);
                cih = request.getRequestDispatcher("index.jsp");
                System.out.println("####################TRUE############################");
            } else {
                request.setAttribute("mensagem", "Professor não encontrado no sistema.");
                cih = request.getRequestDispatcher("login.jsp");
                System.out.println("####################FALSE############################");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        cih.forward(request, response);
    }
}
